package com.imooc.springcloud.biz;

import com.imooc.springcloud.topic.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @describte:TODO
 * @program:spring-cloud-demo
 * @author:zhengdp
 * @create:2021-10-09 17:37:47
 **/
@RestController
@Slf4j
public class Controller {

    @Autowired
    private MyTopic producer;

    @Autowired
    private GroupTopic groupTopicProducer;

    @Autowired
    private DelayedTopic delayedTopicProducer;

    @Autowired
    private ErrorTopic errorTopicProducer;

    @Autowired
    private RequeueTopic requeueTopicProducer;

    @PostMapping("send")
    public void sendMessage(@RequestParam("body") String body){
        producer.output().send(MessageBuilder.withPayload(body).build());
    }

    @PostMapping("sendToGroup")
    public void sendToGroup(@RequestParam("body") String body){
        groupTopicProducer.output().send(MessageBuilder.withPayload(body).build());
    }

    @PostMapping("sendDM")
    public void sendDelayedMessage(@RequestParam("body") String body,
                                   @RequestParam("seconds")Integer seconds){
        MessageBean messageBean = new MessageBean();
        messageBean.setPayload(body);
        log.info("ready to send delayed message");
        delayedTopicProducer.output().
                send(MessageBuilder.
                        withPayload(messageBean)
                        .setHeader("x-delay",1000*seconds)
                        .build());
    }

    // 异常重试，单机版（只在本机重试）
    @PostMapping("sendError")
    public void sendErrorMessage(@RequestParam("body") String body){
        MessageBean messageBean = new MessageBean();
        messageBean.setPayload(body);
        errorTopicProducer.output().send(MessageBuilder.
                                        withPayload(messageBean).
                                        build());
    }

    // 异常重试，Re-queue
    @PostMapping("requeue")
    public void sendErrorMessageToMQ(@RequestParam("body") String body){
        MessageBean messageBean = new MessageBean();
        messageBean.setPayload(body);
        requeueTopicProducer.output().send(MessageBuilder.
                withPayload(messageBean).
                build());
    }

    @Autowired
    private DlqTopic dlqTopicProducer;
    // 死信队列测试
    @PostMapping("dlq")
    public void sendMessageToDlq(@RequestParam("body") String body){
        MessageBean messageBean = new MessageBean();
        messageBean.setPayload(body);
        dlqTopicProducer.output().send(MessageBuilder.
                withPayload(messageBean).
                build());
    }

    @Autowired
    private FallbackTopic fallbackTopicProducer;
    // fallback + 升版
    @PostMapping("fallback")
    public void sendMessageToFallback(@RequestParam("body") String body,
                                      @RequestParam("version")String version){
        MessageBean messageBean = new MessageBean();
        messageBean.setPayload(body);
        // place order
        // placeOrderV1
        // 指定要调用的哪个版本
        fallbackTopicProducer.output().send(
                MessageBuilder.withPayload(messageBean).
                        setHeader("version",version)
                        .build());
    }

}
